<!-- 首页 -->
<template>
	<view class="page" v-if="my == 'true'">
		<u-navbar title="拼团列表"></u-navbar>
		<mescroll-uni ref="mescrollGameRef" height="100%" @init="mescrollInit" @down="downCallback" :up="upOption" @up="upCallback">
			<!-- <view :style="{paddingTop: CustomBar + 'px'}"></view> -->
			<view class="bg-box">
				<view class="tuan-box-border" v-for="(item, index) in dataList" :key="index">
					<view class="tuan-box">
						<view :class="item.tuanType == '1' ? 'vip-brand' : 'tuan-brand'">{{item.tuanTag}}</view>
						<view class="left-box">
							<view class="tuan-title">{{item.title}}</view>
							<view class="price-box">
								<view class="price-tag blue">原价{{item.price}}元</view>
								<view class="price-tag red">拼团价{{item.payPrice}}元</view>
							</view>
							<view class="tuan-max-box">
								还差{{item.chaNum}}人
							</view>
						</view>
						<view class="right-box">
							<view class="user-num">
								<view>截止时间</view>
								<u-count-down :timestamp="item.time"></u-count-down>
							</view>
							<view class="tuan-btn" @click="addTuan(item)">立即加入</view>
						</view>
					</view>
				</view>
				
			</view>
		</mescroll-uni>
		<user-auth></user-auth>
	</view>
</template>

<script>
import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
import MescrollMoreItemMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-more-item.js";
export default {
	mixins: [MescrollMixin,MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
	components: {
		
	},
	props:{
		
	},
	data() {
		return {
			CustomBar: this.CustomBar, 
			upOption:{
				auto:false, // 不自动加载
				// page: {
				// 	num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
				// 	size: 10 // 每页数据的数量
				// },
				noMoreSize: 0, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
				empty:{
			        icon: '/static/mescroll-empty.png',
					tip: '~ 暂无数据 ~', // 提示
					// btnText: '去看看'
				},
				toTop: {
					right: 40,
					width: 80,
					bottom: 200,
				},
				bgColor: '#faf1f1',
				textColor: '#606266',
			    textNoMore:'没有更多了'
			},
			gameList: [],
			dataList: [],
			timestamp: 86400,
			list: [
				'寒雨连江夜入吴',
				'平明送客楚山孤',
				'洛阳亲友如相问',
				'一片冰心在玉壶'
			],
			tuanId: '',
			my: 'false',
		};
	},
	onLoad(option) {
		this.tuanId = option.tuanId;
		this.my = option.my;
	},
	methods: {
		downCallback(mescroll) {
			mescroll.endSuccess();
			mescroll.resetUpScroll();
		},
		async upCallback(mescroll) {
			this.$u.get('/wq/tuanHome/getList',{
				pageNum: mescroll.num,
				pageSize: mescroll.size,
			}).then(res => {
				mescroll.endSuccess(res.rows.length);
				if (mescroll.num == 1) {
					this.dataList = res.rows;
				} else {
					this.dataList = this.dataList.concat(res.rows);
				}
			}).catch(err => {
				mescroll.endErr();
			});
		},
		change(e) {
			this.current = e;
		},
		init(e) {
			this.mescroll.resetUpScroll();
		},
		getGameList() {
			this.$u.get('/wq/miniGame/list',{
				pageNum: 1,
				pageSize: 3,
			}).then(res => {
				this.gameList = res.rows;
			});
		},
		addTuan(e) {
			this.$u.route({
				url: 'packgeMine/pages/tuan/tuan',
				params: {
					my: 'true',
					tuanHomeId: e.id,
				}
			});
		},
		roleOk() {
			uni.$emit('game', true);
		},
		reload() {
			this.query();
		},
		load() {
			this.loadmore();
		},
		toWeb() {
			uni.navigateTo({
				url:'/pages/index/web'
			});
		},
		moreGame() {
			this.$u.route({
				url: 'packgeMine/pages/game/more',
				params: {
					my: 'true',
				}
			});
		},
		qunPlay(e) {
			this.$u.route({
				url: 'packgeMine/pages/game/dynamic',
				params: {
					dynamicId: e.adId,
					back: true,
					my: 'true',
				}
			});
		},
		gamePlay(e) {
			this.$u.get('/wq/miniGame/getAppId',{
				id: e.id,
			}).then(res => {
				if(res.data.appId){
					uni.navigateToMiniProgram({
						appId: res.data.appId,
						success: res => {
							 console.log("打开成功", res);
						},
						fail: err => {
							 console.log(err);
						}
					});
				}else{
					uni.previewImage({
						current: 0, //预览图片的下标
						urls: res.data.img, //预览图片的地址，必须要数组形式，如果不是数组形式就转换成数组形式就可以
						indicator: 'number',
						loop: true
					});
				}
			});
		},
		goHome() {
			this.$u.route({
				url: 'pages/index/test2',
				params: {
					my: 'true',
					back: true,
				}
			});
		},
		loadmore() {
			this.status = 'loading';
			if(this.isEnd){
				this.status = 'nomore';
				return;
			}
			this.$u.get('/wq/game/list',{
				pageNum: this.current,
				pageSize: this.pageSize,
			}).then(res => {
				var totalPage = Math.ceil(res.total/this.pageSize);
				this.pages = totalPage;
				this.current ++;
				this.status = 'loadmore';
				if(this.current > this.pages){
					this.status = 'nomore';
					this.isEnd = true;
				}
				
				this.dataList = this.dataList.concat(res.rows);
			}).catch(err => {
				
			})
		},
		query() {
			this.current = 1;
			this.isEnd = false;
			this.status = 'loadmore';
			this.loadquery();
			this.$emit('fresh');
		},
		loadquery() {
			this.$u.get('/wq/game/list',{
				pageNum: this.current,
				pageSize: this.pageSize,
			}).then(res => {
				var totalPage = Math.ceil(res.total/this.pageSize);
				this.pages = totalPage;
				this.current ++;
				this.status = 'loadmore';
				if(this.current > this.pages){
					this.status = 'nomore';
					this.isEnd = true;
				}
				
				this.dataList = res.rows;
			}).catch(err => {
				
			})
		},
		chat(e) {
			this.$u.route({
				url: 'packgeMine/pages/chat/chat',
				params: {
					roomId: e.roomId,
				}
			});
		},
		reply() {
			this.$u.route({
				url: 'packgeMine/pages/index/reply',
			});
		},
		zhaohu(e) {
			this.judgeLogin((res) => {
				this.$u.route({
					url: 'packgeMine/pages/chat/chat',
					params: {
						roomId: e.roomId,
					}
				});
				//this.test(res,e);
			});
		},
		test(data, e) {
			var that = this;
			var tmplId = data.tmplIds[0];
			wx.getSetting({
			  withSubscriptions: true,
			  success(res) {
				console.log(res)
				if (res.subscriptionsSetting && res.subscriptionsSetting.mainSwitch) {
				  if (res.subscriptionsSetting.itemSettings && res.subscriptionsSetting.itemSettings[tmplId]) {
					let item = res.subscriptionsSetting.itemSettings[tmplId]
					if (item == "reject") {
					  uni.showModal({
					  	title: '温馨提示',
					  	content: '请打开通知管理里面的站内信提醒',
					  	showCancel: false,
					  	success: function(res) {
					  		uni.openSetting({
							  success(res) {
								console.log(res.authSetting)
							  },
							  fail(err) {
								console.error(err);
							  }
							});
					  	}
					  });
					} else if (item == "accept") {
					  that.subscribeMessage(data,e);
					} else if (item == "ban") {
					  uni.showModal({
					  	title: '温馨提示',
					  	content: '请在设置打开订阅消息开关',
					  	showCancel: false,
					  	success: function(res) {
					  		
					  	}
					  });
					}
				  }else{
					that.tipMsg(data, e);
				  }
				} else {
				  uni.showModal({
				  	title: '温馨提示',
				  	content: '请在设置打开通知管理开关',
				  	showCancel: false,
				  	success: function(res) {
				  		uni.openSetting({
						  success(res) {
							console.log(res.authSetting)
						  },
						  fail(err) {
							console.error(err);
						  }
						});
				  	}
				  });
				}
			  }
			});
		},
		userCenter(item) {
			this.judgeLogin(() => {
				this.$u.route({
					url: 'packgeMine/pages/center/index',
					params: {
						userId: item.userId,
					}
				});
			});
		},
		tipMsg(data, e) {
			var that = this;
			uni.showModal({
				title: '温馨提示',
				content: '请勾选总是保持以上选择',
				showCancel: false,
				success: function(res) {
					that.subscribeMessage(data,e);
				}
			});
		},
		subscribeMessage(data,e) {
			var _this = this;
			uni.requestSubscribeMessage({
				//数组，最多放三个订阅模板的ID
				tmplIds: data.tmplIds,
				success: function(res) {
					var code = res[data.tmplIds[0]];
					if('reject' == code){
						_this.$u.toast('请点击允许订阅消息');
					}else{
						_this.$u.route({
							url: 'packgeMine/pages/chat/chat',
							params: {
								roomId: e.roomId,
							}
						});
					}
				},
				fail: function(res) {
					_this.$u.toast('订阅失败');
				},
				complete: (res) => {
					
				}
			});
		},
		dianzan() {
			this.$u.route({
				url: 'pages/index/dianzan',
			});
		},
		fans() {
			this.$u.route({
				url: 'pages/index/fans',
			});
		},
		
	}
}; 
</script>

<style lang="scss" scoped>
	.page {
		background-color: #faf1f1;
	}
	.bg-box {
		padding: 10px;
	}
	.price-box {
		display: flex;
		align-items: center;
		margin-bottom: 10px;
	}
	.tuan-max-box {
		background-color: #eab4b4;
		border-radius: 5px;
		padding: 2px 5px;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		font-size: 11px;
	}
	.tuan-box {
		display: flex;
		align-items: center;
		background-color: #fff;
		border-radius: 5px;
		padding: 15px;
		justify-content: space-between;
		position: relative;
		padding-top: 90rpx;
		border-bottom-left-radius: 0;
		border-bottom-right-radius: 0;
	}
	.left-box {
		display: flex;
		flex-direction: column;
		flex: 1;
	}
	.right-box {
		margin-left: 15px;
		min-width: 70px;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	.tuan-btn {
		background-color: #ff1727;
		border-radius: 40px;
		padding: 18rpx 40rpx;
		color: #fff;
		font-size: 24rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.tuan-join-btn {
		background-color: #ff1727;
		border-radius: 40px;
		padding: 18rpx 40rpx;
		color: #fff;
		font-size: 24rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.user-num {
		color: #ffa6ac;
		font-size: 14px;
		margin-bottom: 10px;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	.tuan-title {
		word-break: break-all;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2; /* 这里是超出几行省略 */
		overflow: hidden;
		font-size: 30rpx;
		margin-bottom: 10px;
		font-weight: bold;
	}
	.price-tag {
		background-color: #ddd;
		border-radius: 20px;
		padding: 3px 10px;
		margin-right: 10px;
		font-size: 22rpx;
		color: #fff;
	}
	.blue {
		background-color: #a6a6ff;
	}
	.red {
		background-color: red;
	}
	.tuan-brand {
		position: absolute;
		top: 0;
		left: 0;
		background-color: #cacca3;
		padding: 3px 20px;
		color: #8f4343;
		border-top-left-radius: 5px;
		border-bottom-right-radius: 5px;
		font-size: 14px;
		font-weight: bold;
	}
	.vip-brand {
		background-color: #edf54e;
		position: absolute;
		top: 0;
		left: 0;
		padding: 3px 20px;
		color: #ff1727;
		border-top-left-radius: 5px;
		border-bottom-right-radius: 5px;
		font-size: 14px;
		font-weight: bold;
	}
	.count-down-box {
		
	}
	.tuan-footer-box {
		background-color: #fff;
		border-radius: 5px;
		padding: 15px;
		border-top-left-radius: 0;
		border-top-right-radius: 0;
	}
	.tuan-box-border {
		margin-bottom: 10px;
	}
</style>
